package command;

import model.impl.TicketImpl;
import DTO.TicketDTO;

import com.gmail.at.vssabinin.train_transport_system_part1.Protocol;
import com.gmail.at.vssabinin.train_transport_system_part1.ReceivedData;

/**
 * Class represents command interface;
 * It sends a message to the client user. Message contains information
 * whether it was successful to sell desirable ticket, and if it was,
 * it sends OK and null, else no and null.
 */
public class ClientSellTicket implements Command {

	private Protocol protocol = new Protocol();
	
	@Override
	public void execute(ReceivedData recived) {
		model.Ticket ticketImpl = new TicketImpl();
		Protocol.Message message = this.protocol.decodeMessage(recived.data);
		TicketDTO ticket = (TicketDTO) message.getObject();
		if (ticketImpl.sellTicket(ticket)){
			Protocol.Message answer = this.protocol.new Message("ok", null);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		} else {
			Protocol.Message answer = this.protocol.new Message("no", null);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		}	
	}
}
